import { _decorator, Camera, Component, instantiate, Node, Prefab, Vec2, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('bloodController')
export class bloodController extends Component {
    @property(Prefab) bloodPrefab: Prefab
    @property(Camera) camera: Camera
    private bloodGroup: Node = null
    private bloodNumber: number = 0

    start() {
        this.bloodGroup = this.node.getChildByName("bloodGroup");
    }

    update(deltaTime: number) {

    }

    setInitBlood(blood:number){
        this.bloodNumber = 0
        this.showBloodUI()
        for (let index = 0; index < blood; index++) {
            this.bloodReturn()
        }
    }

    bloodReturn() {
        this.bloodNumber++
        let blood = instantiate(this.bloodPrefab)
        blood.parent = this.bloodGroup
        blood.name = `blood${this.bloodNumber}`
        const out = new Vec3((this.bloodNumber - 1) * 50, 0, 0)
        blood.setWorldPosition(out)
        const wpos = blood.worldPosition
        this.camera.convertToUINode(wpos,this.bloodGroup,new Vec3((this.bloodNumber - 1) * 50, 0, 0))
        blood.position = out

    }
    bloodLose() {
        this.bloodGroup.removeChild(this.bloodGroup.children[this.bloodNumber-1])
        return --this.bloodNumber
    }
    showBloodUI() {
        this.node.active = true
    }
    hideBloodUI() {
        this.node.active = false
    }
}


